package com.nanjia.boot.warehouse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.warehouse.model.form.WarehouseForm;
import com.nanjia.boot.warehouse.model.query.WarehouseQuery;
import com.nanjia.boot.warehouse.model.vo.WarehouseVO;
import com.nanjia.boot.warehouse.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 仓库信息前端控制层
 *
 * @author 周洋
 * @since 2025-07-04 15:08
 */
@Tag(name = "仓库信息接口")
@RestController
@RequestMapping("/api/v1/warehouse")
@RequiredArgsConstructor
public class WarehouseController  {

    private final WarehouseService warehouseService;

    @Operation(summary = "仓库信息分页列表")
    @GetMapping("/page")
   //@PreAuthorize(@ss.hasPerm('warehouse:warehouse-info:query')")
    public PageResult<WarehouseVO> getWarehousePage(WarehouseQuery queryParams ) {
        IPage<WarehouseVO> result = warehouseService.getWarehousePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增仓库信息")
    @PostMapping
   //@PreAuthorize(@ss.hasPerm('warehouse:warehouse-info:add')")
    public Result<Void> saveWarehouse(@RequestBody @Valid WarehouseForm formData, HttpServletRequest request ) {
        boolean result = warehouseService.saveWarehouse(formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "获取仓库信息表单数据")
    @GetMapping("/{id}/form")
   //@PreAuthorize(@ss.hasPerm('warehouse:warehouse-info:edit')")
    public Result<WarehouseForm> getWarehouseForm(
        @Parameter(description = "仓库信息ID") @PathVariable Long id
    ) {
        WarehouseForm formData = warehouseService.getWarehouseFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改仓库信息")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('warehouse:warehouse-info:edit')")
    public Result<Void> updateWarehouse(
            @Parameter(description = "仓库信息ID") @PathVariable Long id,
            @RequestBody @Validated WarehouseForm formData
    ) {
        boolean result = warehouseService.updateWarehouse(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除仓库信息")
    @DeleteMapping("/{ids}")
   //@PreAuthorize(@ss.hasPerm('warehouse:warehouse-info:delete')")
    public Result<Void> deleteWarehouses(
        @Parameter(description = "仓库信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = warehouseService.deleteWarehouses(ids);
        return Result.judge(result);
    }
}
